package com.dp.appi.service.impl;

import com.dp.appi.dao.PTDao;
import com.dp.appi.service.PTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 定位点特征点通用处理类
 * Created by bchen on 2016/5/17.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PTServiceImpl implements PTService {

    @Autowired
    private PTDao ptDao;

    /**
     * 获取PT字段
     *
     * @param pt_id
     * @return
     */
    @Override
    public List<Map<String, Object>> ptField(int pt_id) {
        return ptDao.ptField(pt_id);
    }

    /**
     * 查询某条定位点扩展信息
     *
     * @param pt_id
     * @param pt_objectid
     * @return
     */
    @Override
    public Map<String, Object> queryPt(int pt_id, Long pt_objectid) {
        return ptDao.queryPt(ptDao.ptTable(pt_id),pt_objectid);
    }

    /**
     * 校对定位点参数
     * @param ptMap
     * @param pt_id
     * @return
     */
    public List<Map<String,Object>> assembledParameter(Map<String, Object> ptMap, int pt_id){
        List<Map<String, Object>> ptFieldList = ptField(pt_id);
        List<Map<String, Object>> ptsFieldList = new ArrayList<>();

        for(Map<String,Object> ptFiledMap : ptFieldList){
            Object column_name = ptFiledMap.get("COLUMN_NAME");
            Object data_type = ptFiledMap.get("DATA_TYPE");
            if(ptMap.containsKey(column_name)) {
                Object value = ptMap.get(column_name);
//                if (null != column_name && null != value) {
                    Map<String, Object> ptsMap = new HashMap<>();
                    ptsMap.put("column_name", column_name);
                    ptsMap.put("column_value", value);
                    ptsMap.put("data_type", data_type);
                    ptsFieldList.add(ptsMap);
//                }
            }
        }
        return ptsFieldList;
    }

    /**
     * 新增定位点扩展信息
     *
     * @param ptMap
     * @param pt_id
     */
    @Override
    public Long newPt(Map<String, Object> ptMap, int pt_id,Long exploration_id) {
        List<Map<String, Object>> ptMapList = assembledParameter(ptMap,pt_id);
        Long id = ptDao.newPt(ptMapList,ptDao.ptTable(pt_id));
        ptDao.upPtObjectid(id,exploration_id);
        return id;
    }

    /**
     * 修改定位点扩展信息
     *
     * @param ptMap
     * @param pt_id
     * @param pt_objectid
     */
    @Override
    public void upPt(Map<String, Object> ptMap, int pt_id, Long pt_objectid) {
        List<Map<String, Object>> ptMapList = assembledParameter(ptMap,pt_id);
        ptDao.upPt(ptMapList,ptDao.ptTable(pt_id),pt_objectid);
    }

    /**
     * 删除定位点扩展信息
     *
     * @param pt_id
     * @param pt_objectid
     */
    @Override
    public void delPt(int pt_id, Long pt_objectid) {
        ptDao.delPt(ptDao.ptTable(pt_id),pt_objectid);
    }

}
